/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.icons;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;

/**
 *
 * @author jannes
 */
public class IconButton extends JLabel{
    private ArrayList<ActionListener> listeners;

    public IconButton(Icon icon){
        super(icon);
        final JLabel thisItem = this;
        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                for(ActionListener listener : listeners){
                    listener.actionPerformed(new ActionEvent(thisItem, 0, "button pressed"));
                }
            }

            public void mousePressed(MouseEvent e) {
                thisItem.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            }

            public void mouseReleased(MouseEvent e) {
                thisItem.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            }

            public void mouseEntered(MouseEvent e) {
                thisItem.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            }

            public void mouseExited(MouseEvent e) {
                thisItem.setBorder(null);
            }
        });
    }

    public void addActionListener(ActionListener actionListener){
        listeners.add(actionListener);
    }

    public void removeActionListener(ActionListener actionListener){
        listeners.remove(actionListener);
    }
}
